package cn.itsource.system.listener;

import cn.itsource.system.util.PermissionScanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * 自定义类 权限扫描监听器
 *
 * spring常识   如果A类没有交给spring管控，但是你在A类想要拿到spring管控的对象 是拿不到的
 */
@WebListener//声明当前类是一个监听器
public class PermissionScanListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        // 使用WebApplicationContextUtils从ServletContext中获取Spring的ApplicationContext
        ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext());
        //获取Spring管控的对象
        PermissionScanUtil permissionScanUtil = context.getBean("permissionScanUtil", PermissionScanUtil.class);
         //开启一个多线程
        new Thread(()->{
            //开始扫描
            permissionScanUtil.scan();
        }).start();
    }
}
